package com.mall.goods.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.goods.mapper.PmsProductMapper;
import com.mall.model.domain.*;
import com.mall.model.dto.PmsProductDto;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.PmsProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PmsProductService extends ServiceImpl<PmsProductMapper, PmsProduct> {

    @Autowired(required = false)
    private PmsProductMapper pmsProductMapper;

    @Autowired(required = false)
    private PmsMemberPriceService pmsMemberPriceService;

    @Autowired(required = false)
    private CmsPrefrenceAreaProductRelationService cmsPrefrenceAreaProductRelationService;

    @Autowired(required = false)
    private PmsProductAttributeValueService pmsProductAttributeValueService;

    @Autowired(required = false)
    private PmsProductFullReductionService pmsProductFullReductionService;

    @Autowired(required = false)
    private PmsProductLadderService pmsProductLadderService;

    @Autowired(required = false)
    private PmsSkuStockService pmsSkuStockService;

    @Autowired(required = false)
    private CmsSubjectProductRelationService cmsSubjectProductRelationService;


    /**
     * 分页查询商品信息
     *
     * @param dto
     * @return
     */
    public CommonPage list(PmsProductDto dto) {
        Page<PmsProduct> pageInfo = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper.like(dto.getKeyword() != null, PmsProduct::getName, dto.getKeyword());
        queryWrapper.eq(dto.getBrandId() != null, PmsProduct::getBrandId, dto.getBrandId());
        queryWrapper.eq(dto.getProductCategoryId() != null, PmsProduct::getProductCategoryId, dto.getProductCategoryId());
        queryWrapper.eq(dto.getProductSn() != null, PmsProduct::getProductSn, dto.getProductSn());
        queryWrapper.eq(dto.getPublishStatus() != null, PmsProduct::getPublishStatus, dto.getPublishStatus());
        queryWrapper.eq(dto.getVerifyStatus() != null, PmsProduct::getVerifyStatus, dto.getVerifyStatus());
        queryWrapper.eq(PmsProduct::getDeleteStatus, 0);
        queryWrapper.orderByDesc(PmsProduct::getSort);
        Integer count = pmsProductMapper.selectCount(queryWrapper);
        pmsProductMapper.selectPage(pageInfo, queryWrapper);
        return new CommonPage(dto.getPageNum(), dto.getPageSize(), count, pageInfo.getRecords());

    }

    /**
     * 根据商品名称或货号模糊查询
     *
     * @param keyword
     * @return
     */
    public List simpleList(String keyword) {
        LambdaQueryWrapper<PmsProduct> qw = new LambdaQueryWrapper<>();
        qw.like(keyword != null, PmsProduct::getName, keyword)
                .or()
                .like(keyword != null, PmsProduct::getProductSn, keyword);
        List resultList = pmsProductMapper.selectList(qw);
        return resultList;
    }

    /**
     * 批量修改删除状态
     *
     * @param deleteStatus 修改删除状态
     * @param ids          需要修改的id
     */
    public void deleteStatus(Integer deleteStatus, List<Long> ids) {
        LambdaQueryWrapper<PmsProduct> qw = new LambdaQueryWrapper<>();
        qw.in(PmsProduct::getId, ids);
        List<PmsProduct> pmsProducts = this.list(qw);
        for (PmsProduct pmsProduct : pmsProducts) {
            pmsProduct.setDeleteStatus(deleteStatus);
        }
        this.updateBatchById(pmsProducts);
    }

    /**
     * 批量设为新品
     *
     * @param newStatus
     * @param ids
     */
    public void newStatus(Integer newStatus, List<Long> ids) {
        LambdaQueryWrapper<PmsProduct> qw = new LambdaQueryWrapper<>();
        qw.in(PmsProduct::getId, ids);
        List<PmsProduct> pmsProducts = this.list(qw);
        for (PmsProduct pmsProduct : pmsProducts) {
            pmsProduct.setNewStatus(newStatus);
        }
        this.updateBatchById(pmsProducts);
    }


    /**
     * 批量上下架商品
     *
     * @param publishStatus
     * @param ids
     */
    public void publishStatus(Integer publishStatus, List<Long> ids) {
        LambdaQueryWrapper<PmsProduct> qw = new LambdaQueryWrapper<>();
        qw.in(PmsProduct::getId, ids);
        List<PmsProduct> pmsProducts = this.list(qw);
        for (PmsProduct pmsProduct : pmsProducts) {
            pmsProduct.setPublishStatus(publishStatus);
        }
        this.updateBatchById(pmsProducts);
    }


    /**
     * 批量推荐商品
     *
     * @param recommendStatus
     * @param ids
     */
    public void recommendStatus(Integer recommendStatus, List<Long> ids) {
        LambdaQueryWrapper<PmsProduct> qw = new LambdaQueryWrapper<>();
        qw.in(PmsProduct::getId, ids);
        List<PmsProduct> pmsProducts = this.list(qw);
        for (PmsProduct pmsProduct : pmsProducts) {
            pmsProduct.setRecommandStatus(recommendStatus);
        }
        this.updateBatchById(pmsProducts);
    }

    /**
     * 创建商品
     *
     * @param pmsProductDto
     */
    public void create(PmsProductDto pmsProductDto) {
        //保存商品信息数据
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductDto, pmsProduct);
        pmsProductMapper.insert(pmsProduct);
        Long productId = pmsProduct.getId();
        //保存商品会员价格表数据memberPricesList
        List<PmsMemberPrice> memberPricesList = pmsProductDto.getMemberPriceList();
        if (!CollUtil.isEmpty(memberPricesList)) {
            memberPricesList = memberPricesList.stream().map(item -> {
                item.setProductId(productId);
                return item;
            }).collect(Collectors.toList());
            pmsMemberPriceService.saveBatch(memberPricesList);
        }
        //保存优选专区和产品关系表prefrenceAreaProductRelationList
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsProductDto.getPrefrenceAreaProductRelationList();
        if (!CollUtil.isEmpty(prefrenceAreaProductRelationList)) {
            prefrenceAreaProductRelationList = prefrenceAreaProductRelationList.stream().map(item -> {
                item.setProductId(productId);
                return item;
            }).collect(Collectors.toList());
            cmsPrefrenceAreaProductRelationService.saveBatch(prefrenceAreaProductRelationList);
        }
        //保存存储产品参数信息的表PmsProductAttributeValue
        List<PmsProductAttributeValue> productAttributeValueList = pmsProductDto.getProductAttributeValueList();
        if (!CollUtil.isEmpty(productAttributeValueList)) {
            productAttributeValueList = productAttributeValueList.stream().map(item -> {
                item.setProductId(productId);
                return item;
            }).collect(Collectors.toList());
            pmsProductAttributeValueService.saveBatch(productAttributeValueList);
        }
        //保存产品满减表PmsProductFullReduction
        List<PmsProductFullReduction> productFullReductionList = pmsProductDto.getProductFullReductionList();
        if (!CollUtil.isEmpty(productFullReductionList)) {
            productFullReductionList = productFullReductionList.stream().map(item -> {
                item.setProductId(productId);
                return item;
            }).collect(Collectors.toList());
            pmsProductFullReductionService.saveBatch(productFullReductionList);
        }
        //保存产品阶梯价格表
        List<PmsProductLadder> productLadderList = pmsProductDto.getProductLadderList();
        if (!CollUtil.isEmpty(productLadderList)) {
            productLadderList = productLadderList.stream().map(item -> {
                item.setProductId(productId);
                return item;
            }).collect(Collectors.toList());
            pmsProductLadderService.saveBatch(productLadderList);
        }
        //保存sku的库存数据
        List<PmsSkuStock> skuStockList = pmsProductDto.getSkuStockList();
        if (!CollUtil.isEmpty(skuStockList)) {
            skuStockList = skuStockList.stream().map(item -> {
                item.setProductId(productId);
                return item;
            }).collect(Collectors.toList());
            pmsSkuStockService.saveBatch(skuStockList);
        }
        //保存专题商品关系表
        List<CmsSubjectProductRelation> subjectProductRelationList = pmsProductDto.getSubjectProductRelationList();
        if (!CollUtil.isEmpty(subjectProductRelationList)) {
            subjectProductRelationList = subjectProductRelationList.stream().map(item -> {
                item.setProductId(productId);
                return item;
            }).collect(Collectors.toList());
            cmsSubjectProductRelationService.saveBatch(subjectProductRelationList);
        }
    }

    /**
     * 更新商品
     *
     * @param
     * @param pmsProductDto
     */
    public void updateProduct(PmsProductDto pmsProductDto) {
        //保存商品信息数据
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductDto, pmsProduct);
        pmsProductMapper.updateById(pmsProduct);
        //保存商品会员价格表数据memberPricesList
        List<PmsMemberPrice> memberPricesList = pmsProductDto.getMemberPriceList();
        pmsMemberPriceService.updateBatchById(memberPricesList);
        //保存优选专区和产品关系表prefrenceAreaProductRelationList
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsProductDto.getPrefrenceAreaProductRelationList();
        cmsPrefrenceAreaProductRelationService.updateBatchById(prefrenceAreaProductRelationList);
        //保存存储产品参数信息的表PmsProductAttributeValue
        List<PmsProductAttributeValue> productAttributeValueList = pmsProductDto.getProductAttributeValueList();
        pmsProductAttributeValueService.updateBatchById(productAttributeValueList);
        //保存产品满减表PmsProductFullReduction
        List<PmsProductFullReduction> productFullReductionList = pmsProductDto.getProductFullReductionList();
        pmsProductFullReductionService.updateBatchById(productFullReductionList);
        //保存产品阶梯价格表
        List<PmsProductLadder> productLadderList = pmsProductDto.getProductLadderList();
        pmsProductLadderService.updateBatchById(productLadderList);
        //保存sku的库存数据
        List<PmsSkuStock> skuStockList = pmsProductDto.getSkuStockList();
        pmsSkuStockService.updateBatchById(skuStockList);
        //保存专题商品关系表
        List<CmsSubjectProductRelation> subjectProductRelationList = pmsProductDto.getSubjectProductRelationList();
        cmsSubjectProductRelationService.updateBatchById(subjectProductRelationList);
    }

    /**
     * 根据商品id获取商品编辑信息
     *
     * @param id
     * @return
     */
    public PmsProductVO updateInfo(Integer id) {
        LambdaQueryWrapper<PmsProduct> pmsProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsProductLambdaQueryWrapper.eq(PmsProduct::getId, id);
        PmsProduct pmsProduct = pmsProductMapper.selectOne(pmsProductLambdaQueryWrapper);
        //保存商品会员价格表数据memberPricesList
        LambdaQueryWrapper<PmsMemberPrice> pmsMemberPriceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsMemberPriceLambdaQueryWrapper.eq(PmsMemberPrice::getProductId, id);
        List<PmsMemberPrice> memberPriceList = pmsMemberPriceService.list(pmsMemberPriceLambdaQueryWrapper);
        //保存优选专区和产品关系表prefrenceAreaProductRelationList
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cmsPrefrenceAreaProductRelationLambdaQueryWrapper.eq(CmsPrefrenceAreaProductRelation::getProductId, id);
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = cmsPrefrenceAreaProductRelationService.list(cmsPrefrenceAreaProductRelationLambdaQueryWrapper);
        //保存存储产品参数信息的表PmsProductAttributeValue
        LambdaQueryWrapper<PmsProductAttributeValue> pmsProductAttributeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsProductAttributeValueLambdaQueryWrapper.eq(PmsProductAttributeValue::getProductId, id);
        List<PmsProductAttributeValue> pmsProductAttributeValueList = pmsProductAttributeValueService.list(pmsProductAttributeValueLambdaQueryWrapper);
        //保存产品满减表PmsProductFullReduction
        LambdaQueryWrapper<PmsProductFullReduction> pmsProductFullReductionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsProductFullReductionLambdaQueryWrapper.eq(PmsProductFullReduction::getProductId, id);
        List<PmsProductFullReduction> productFullReductionList = pmsProductFullReductionService.list(pmsProductFullReductionLambdaQueryWrapper);
        //保存产品阶梯价格表
        LambdaQueryWrapper<PmsProductLadder> pmsProductLadderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsProductLadderLambdaQueryWrapper.eq(PmsProductLadder::getProductId, id);
        List<PmsProductLadder> pmsProductLadderList = pmsProductLadderService.list(pmsProductLadderLambdaQueryWrapper);
        //保存sku的库存数据
        LambdaQueryWrapper<PmsSkuStock> pmsSkuStockLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsSkuStockLambdaQueryWrapper.eq(PmsSkuStock::getProductId, id);
        List<PmsSkuStock> skuStockList = pmsSkuStockService.list(pmsSkuStockLambdaQueryWrapper);
        //保存专题商品关系表
        LambdaQueryWrapper<CmsSubjectProductRelation> cmsSubjectProductRelationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cmsSubjectProductRelationLambdaQueryWrapper.eq(CmsSubjectProductRelation::getProductId, id);
        List<CmsSubjectProductRelation> subjectProductRelationList = cmsSubjectProductRelationService.list(cmsSubjectProductRelationLambdaQueryWrapper);

        PmsProductVO pmsProductVO = new PmsProductVO();
        BeanUtils.copyProperties(pmsProduct, pmsProductVO);
        pmsProductVO.setMemberPriceList(memberPriceList);
        pmsProductVO.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList);
        pmsProductVO.setProductAttributeValueList(pmsProductAttributeValueList);
        pmsProductVO.setProductFullReductionList(productFullReductionList);
        pmsProductVO.setProductLadderList(pmsProductLadderList);
        pmsProductVO.setSkuStockList(skuStockList);
        pmsProductVO.setSubjectProductRelationList(subjectProductRelationList);
        return pmsProductVO;
    }
}
